package com.decent.ejfadmin.utils;

import com.aliyun.openservices.shade.com.alibaba.rocketmq.shade.com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicInteger;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * describe:缓存utils
 *
 * @author 陈豆豆
 * @date 2019/09/07
 */
@Slf4j
@Component
public class SimpleCacheUtil {

    private static RedisTemplate<Object, Object> redisTemplate;

    @Autowired
    public SimpleCacheUtil(RedisTemplate<Object, Object> redisTemplate) {
        SimpleCacheUtil.redisTemplate = redisTemplate;
    }

    public static RedisTemplate getRedisTemplate() {
        return redisTemplate;
    }

    public static void setObject(Object k, Object v, long seconds) {
        try {
            redisTemplate.opsForValue().set(k, v, seconds, TimeUnit.SECONDS);
        } catch (Exception e) {
        }

    }

    public static void setObjectJson(Object k, Object v, long seconds) {
        try {
            String stringMapper = JSONObject.toJSONString(v);
            redisTemplate.opsForValue().set(k, stringMapper, seconds, TimeUnit.SECONDS);
        } catch (Exception e) {
        }
    }

    public static Object getObjectJson(Object k, Class c) {
        try {
            Object object = redisTemplate.opsForValue().get(k);
            return JSONObject.parseObject(String.valueOf(object), c);
        } catch (Exception e) {
            return null;
        }
    }

    public static void setObject(Object k, Object v) {
        try {
            String stringMapper = JSONObject.toJSONString(v);
            redisTemplate.opsForValue().set(k, stringMapper);
        } catch (Exception e) {
            log.error("redis setObject{}", e, e);
        }
    }

    public static void removeObject(Object k) {
        redisTemplate.delete(k);
    }

    public static Object getObject(Object token) {
        try {
            Object object = redisTemplate.opsForValue().get(token);
            return object;
        } catch (Exception e) {

        }
        return null;
    }

    public static Object getObjectClazz(Object token, Class c) {
        try {
            Object object = redisTemplate.opsForValue().get(token);
            object = JSONObject.parseObject(String.valueOf(object), c);
            return object;
        } catch (Exception e) {

        }
        return null;
    }

    /**
     * 6库（注册信息）
     *
     * @param token 黑名单ip/号码
     * @return 黑名单
     */
    public static Object getKeyObject(Object token) {
        try {
            Object object = redisTemplate.opsForValue().get(token);
            return object;
        } catch (Exception e) {

        }
        return null;
    }

    /**
     * 添加缓存,已存在就返回
     *
     * @param key      键
     * @param value    值
     * @param time     保存时长
     * @param timeUnit 时间计算方式
     */
    public static Object checkAndSet(Object key, Object value, int time, TimeUnit timeUnit) {
        Object redisValue = redisTemplate.opsForValue().get(key);
        if (redisValue != null) {
            return redisValue;
        }
        redisTemplate.opsForValue().set(key, value, (int) timeUnit.toSeconds(time), timeUnit);
        return null;
    }

    /**
     * 自增
     *
     * @param key      键
     * @param time     保存时长
     * @param timeUnit 时间计算方式
     * @return 值
     */
    public static Long incrWithTimeOut(String key, int time, TimeUnit timeUnit) {
        RedisAtomicInteger redisAtomicInteger = new RedisAtomicInteger(key, redisTemplate.getConnectionFactory());
        redisAtomicInteger.expire(time, timeUnit);
        return (long) redisAtomicInteger.getAndIncrement();
    }

    /**
     * 自减
     *
     * @param key
     * @return
     */

    public static Long decr(String key) {
        return Long.valueOf(new RedisAtomicInteger(key, redisTemplate.getConnectionFactory()).getAndIncrement());
    }

}
